package com.lijiajia.cloud.common.core.jackson.databind.converter;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

/**
 * OffsetDateTime 序列化
 *
 * @author lijiajia
 * @since 2019-4-24
 */
public class CustomOffsetDateTimeSerializer extends JsonSerializer<OffsetDateTime> {

    public static final CustomOffsetDateTimeSerializer INSTANCE = new CustomOffsetDateTimeSerializer();

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");

    @Override
    public void serialize(OffsetDateTime offsetDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(offsetDateTime.toLocalTime().format(formatter));
    }
}
